package com.shchuangzhi.smartguard.core.receiver;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shchuangzhi.smartguard.R;

import java.util.List;

/**
 * Created by j_hu on 11/2/17.
 */

public class ReceiverGridViewAdapter extends ArrayAdapter<ReceiverInOutData.InOutDataView> {
    private Context context;
    private List<ReceiverInOutData.InOutDataView> data;

    public ReceiverGridViewAdapter(Context context, List<ReceiverInOutData.InOutDataView> data) {
        super(context, R.layout.receiver_grid_item_layout, data);
        this.context = context;
        this.data = data;
    }

    public void reload() {
        data = ReceiverInOutData.getInstance().getDataViewList();
        if (data == null || data.size() == 0) {
            clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder holder;

        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.receiver_grid_item_layout, parent, false);
            holder = new ViewHolder();
            holder.tvName = (TextView)view.findViewById(R.id.receiver_student_name);
            holder.imgPhoto = (ImageView)view.findViewById(R.id.receiver_student_photo);
            holder.imgState = (ImageView)view.findViewById(R.id.receiver_student_state);
            view.setTag(holder);
        } else {
            holder = (ViewHolder)view.getTag();
        }

        if (position <= data.size() - 1) {
            ReceiverInOutData.InOutDataView item = data.get(position);
            holder.tvName.setText(String.format("%02d%s", item.getNo() % 100, item.getName()));
            item.displayImage(holder.imgPhoto, holder.imgState);
        }

        return view;
    }

    static class ViewHolder {
        TextView tvName;
        ImageView imgPhoto;
        ImageView imgState;
    }
}
